package com.hoyue.classroom.controller;

import com.hoyue.classroom.bean.Course;
import com.hoyue.classroom.bean.Group;
import com.hoyue.classroom.service.GroupService;
import com.hoyue.classroom.service.MemberService;
import com.hoyue.common.utils.RUtils;
import com.hoyue.common.utils.ShiroUtils;
import com.hoyue.system.bean.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author 空白
 * @version 1.0
 * Create by 2019/09/01 16:40
 */
@Controller
@RequestMapping("/group")
public class GroupController {

    private String prefix = "/group";

    @Autowired
    GroupService groupService;

    @Autowired
    MemberService memberService;

    /**
     * 查询此课程的所有小组
     *
     * @param model
     * @param session
     * @return
     */
    @GetMapping("/list")
    public String GroupList(Model model, HttpSession session) {
        Course course = (Course) session.getAttribute("course");
        // 查询此课程下所有成员
        int cnt = memberService.ListMember(course.getId()).size();
        // 找到该课程下的所有分组
        List<Group> groupList = groupService.listGroup(course.getId());
        for (Group group : groupList) {
            // 查询小组成员
            List<User> students = groupService.listGroupMember(group.getId());
            // 获取小组成员人数
            cnt -= students.size();
            group.setNumber(students.size());
            group.setStudents(students);
        }
        model.addAttribute("cnt", cnt);
        model.addAttribute("groupsSize", groupList.size());
        model.addAttribute("groups", groupList);
        return prefix + "/group";
    }

    /**
     * 跳转到添加小组页面
     * @return
     */
    @GetMapping("/add")
    public String addGroup() {
        return prefix + "/add";
    }

    /**
     * 添加小组
     *
     * @param name
     * @param session
     * @return
     */
    @ResponseBody
    @PostMapping("/create")
    public RUtils createGroup(String name, HttpSession session) {
        // 获取当前用户信息
        Integer userId = ShiroUtils.getUserId();
        // 获取课程信息
        Course course = (Course) session.getAttribute("course");

        if (groupService.createGroup(name, userId, course.getId()) > 0) {
            return RUtils.ok();
        } else {
            return RUtils.error(1, "添加失败");
        }
    }

    /**
     * 跳转到编辑页面
     * @param model
     * @param id 小组id
     * @return
     */
    @GetMapping("/edit/{id}")
    public String editGroup(Model model, @PathVariable("id") Integer id) {
        Group group = groupService.getGroup(id);
        model.addAttribute("group", group);
        return prefix + "/edit";
    }

    /**
     * 提交小组更新信息
     * @param groupId
     * @param name
     * @return
     */
    @ResponseBody
    @PostMapping("/update")
    public RUtils updateGroup(Integer groupId, String name) {
        if (groupService.updateGroup(groupId, name) > 0) {
            return RUtils.ok();
        } else {
            return RUtils.error(1, "修改小组信息失败");
        }
    }
}
